/* __auth__ : KunLin */

package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Reference(timeout = 30000,retries = 2, check = false)
    OrderCoreService orderCoreService;

    @Reference(timeout = 30000,retries = 2, check = false)
    OrderQueryService orderQueryService;


    @RequestMapping(value = {"/order"}, method = {RequestMethod.GET})
    public ResponseData<OrderListResponse> queryOrder(OrderListRequest orderListRequest, HttpServletRequest request){
//        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject object = JSON.parseObject(userInfo);
//        Long uid = Long.parseLong(object.get("uid").toString());
        Long uid = (Long) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        orderListRequest.setUserId(uid);

        OrderListResponse response = orderQueryService.queryOrder(orderListRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<OrderListResponse>().setData(response);
        }
        return new ResponseUtil<OrderListResponse>().setErrorMsg(response.getMsg());
    }


    @RequestMapping(value = {"/order/{id}"}, method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<OrderIdDetailResponse> queryOrderDetail(@PathVariable("id") String orderId, HttpServletRequest request){
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);

        OrderIdDetailResponse response = orderQueryService.queryOrderDetail(orderDetailRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<OrderIdDetailResponse>().setData(response);
        }
        return new ResponseUtil<OrderIdDetailResponse>().setErrorMsg(response.getMsg());
    }



    @RequestMapping(value = {"/cancelOrder"}, method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<CancelOrderResponse> cancelOrder(@RequestBody CancelOrderRequest request){

        CancelOrderResponse response = orderCoreService.cancelOrder(request);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<CancelOrderResponse>().setData(response);
        }
        return new ResponseUtil<CancelOrderResponse>().setErrorMsg(response.getMsg());
    }


    @RequestMapping(value = {"/order/{id}"}, method = {RequestMethod.DELETE})
    @ResponseBody
    @Anoymous
        public ResponseData<DeleteOrderResponse> deleteOrder(@PathVariable("id") String orderId, HttpServletRequest request){
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(orderId);


        DeleteOrderResponse response = orderCoreService.deleteOrder(deleteOrderRequest);


        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())){
            return new ResponseUtil<DeleteOrderResponse>().setData(response);
        }
        return new ResponseUtil<DeleteOrderResponse>().setErrorMsg(response.getMsg());
    }

    /**
     * lxy
     * 创建订单
     */
    @PostMapping("/order")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        // 从request域中拿到用户信息
        //String userInfo = String.valueOf(servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY));
        //String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //JSONObject object = JSON.parseObject(userInfo);
        //Long uid = Long.parseLong(object.get("uid").toString());
        Long uid = (Long) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        request.setUserId(uid);
        // 设置uniqueKey
        request.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

}
